<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
  Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="EpsonScanSDK.js"></script>
    <script language="javascript" type="text/javascript">

        var eslObj = ESLFunctions.ESLCreate();

        var currnet_destination = ESLFunctions.DEST_FILE;
        var current_fileFormat = ESLFunctions.FF_PDF;
        var current_count = 0;

        function viewFile(base64)
        {
            var link = document.createElement('a');
            if (current_fileFormat == ESLFunctions.FF_PDF)
            {
                link.download = 'file'+current_count+'.pdf';
            }
            if (current_fileFormat == ESLFunctions.FF_JPEG)
            {
                link.download = 'file'+current_count+'.jpg';
            }
            if (current_fileFormat == ESLFunctions.FF_MULTITIFF)
            {
                link.download = 'file'+current_count+'.tif';
            }
            if (current_fileFormat == ESLFunctions.FF_TIFF)
            {
                link.download = 'file'+current_count+'.tif';
            }
            if (current_fileFormat == ESLFunctions.FF_BMP)
            {
                link.download = 'file'+current_count+'.bmp';
            }
            link.innerHTML = link.download;
            link.href = 'data:application/octet-stream;base64,' + base64;

            var downloadArea = document.getElementById('downloadarea');

            document.body.insertBefore(document.createElement('br'), downloadArea);
            document.body.insertBefore(link, downloadArea);

            current_count+=1;
        }

        function printFilePath(filePath)
        {
            var downloadArea = document.getElementById('downloadarea');

            document.body.insertBefore(document.createTextNode(filePath), downloadArea);
            document.body.insertBefore(document.createElement('br'), downloadArea);
        }

        function writeLog(message) {
            var log = document.getElementById('log');
            log.value += message + "\n";
            log.scrollTop = log.scrollHeight;
        }

        function OpenButtonClick() {
            var params = {};
            params.deviceName = "DS-530II";
            params.connectType = 0;

            eslObj.Open(params, function(isSuccess, result){
                if (isSuccess == true)
                {
                    writeLog("open success");
                    document.getElementById("open").disabled = true;
                    document.getElementById("scan").disabled = false;
                    document.getElementById("cancel").disabled = false;
                    document.getElementById("close").disabled = false;
                    document.getElementById("gserialNumber").disabled = false;

                }else{
                    writeLog("open fail");
                }
            } );
        }


        function ScanButtonClick() {
            var params = {};
            params.docSource = parseInt(document.DocSource.DocSource.value, 10);     /*ESLFunctions.SRC_AUTO*/;
            params.imgType = parseInt(document.ImageType.ImageType.value, 10); //ESLFunctions.IT_AUTO;
            params.resolution = parseInt(document.Resolution.Resolution.value, 10);//200
            params.docSize =  parseInt(document.Size.Size.value, 10);
            params.docRotate = ESLFunctions.DR_NONE;
            params.brightness = 0;
            params.contrast = 0;
            params.threshold = 126;
            params.colorFilter = ESLFunctions.CF_NONE;
            params.numScan = 0;//ALL
            params.docEnhance = 0;
            params.imgFilter = 0;
            params.optDoubleFeedDetect = ESLFunctions.DFD_NONE;
            params.optBlankPageSkip = ESLFunctions.BPS_NONE;
            params.skewCorrect = ESLFunctions.SC_EDGE;
            params.optPunchHoleRemove = ESLFunctions.PHR_ON;


            eslObj.Scan(params, function(isSuccess, result){
                if (isSuccess)
                {
                    if (result.eventType == ESLFunctions.EVENT_SCANPAGE_COMPLETE)
                    {
                        writeLog("scanning page " + result.page);
                    }
                    if (result.eventType == ESLFunctions.EVENT_ALLSCAN_COMPLETE)
                    {
                         writeLog("All Scan Complete");
                         document.getElementById("save").disabled = false;
                    }
                }else{
                    writeLog("scan request fail");
                }
            });
        }

        function SaveButtonClick() {

            currnet_destination = document.destination.destination.selectedIndex;
            current_fileFormat = document.fileFormat.fileFormat.selectedIndex;

            var params = {};
            params.destination = currnet_destination;
            params.fileFormat = current_fileFormat; 
            params.fileTimeFormat = 3;

            if (currnet_destination == ESLFunctions.DEST_FILE)
            {
                params.filePath = "C:\\ProgramData\\epson";
                params.fileNamePrefix = "TEST";
            }
            if (current_fileFormat == ESLFunctions.FF_PDF)
            {
                params.addOcrText = 1;
                params.ocrLanguage = 0x10;
            }
            if (current_fileFormat == ESLFunctions.FF_JPEG)
            {
                params.imgQuality = 80;
            }

            eslObj.Save(params, function(isSuccess, result){
                if (isSuccess == true)
                {
                     if (result.eventType == ESLFunctions.EVENT_SAVEPAGE_COMPLETE)
                     {
                        writeLog("saved");
                        if (currnet_destination == ESLFunctions.DEST_BASE64)
                        {
                            viewFile(result.base64);
                        }else{
                            printFilePath(result.filePath);
                        }
                     }
                    if (result.eventType == ESLFunctions.EVENT_ALLSAVE_COMPLETE)
                    {
                        writeLog("All saving complete");
                    }                                    
                }else{
                    writeLog("save request fail");
                }
            });
        }

        function CloseButtonClick() {

            if (eslObj == null)
            {
                return;
            }

            eslObj.Close(function(isSuccess, result){
                if (isSuccess == true)
                {
                    writeLog("close success");
                    document.getElementById("scan").disabled = true;
                    document.getElementById("close").disabled = true;
                    document.getElementById("save").disabled = true;
                    document.getElementById("open").disabled = false;
                    document.getElementById("cancel").disabled = true;
                    document.getElementById("gserialNumber").disabled = true;
                }else{
                    writeLog("close fail");
                }
            });
        }

        function CancelButtonClick() {

            if (eslObj == null)
            {
                return;
            }

            eslObj.Cancel(function(isSuccess, result){
                if (isSuccess == true)
                {
                    writeLog("cancel success");
                }else{
                    writeLog("cancel fail");
                }
            });
        }
        function GetSerialButtonClick() {
            if (eslObj == null)
            {
                return;
            }

            eslObj.GetInfo(function(isSuccess, result){
                if (isSuccess == true)
                {
                    writeLog("GetInfo success");
                    writeLog("serialNumber =" + result.serialNumber);

                }else{
                    writeLog("GetInfo fail");
                }
            });

        }

        window.onbeforeunload = function(){
            CloseButtonClick();
            return false;
        }

    </script>
</head>
<body>
    <h3> Epson Scan SDK Web Extension Sample </h3>
    <br />
    <input type="button" id="open" value="Open" onclick="OpenButtonClick();"/><br />
    <br />

    <table>
            <tr>
                <td>Image Type :</td>
                <td>
                        <form name="ImageType">
                            <select name="ImageType">
                                <option value="-1" selected="selected">IT_AUTO</option>
                                <option value="0">IT_COLOR</option>
                                <option value="1">IT_GRAY</option>
                                <option value="2">IT_MONO</option>
                            </select>
                        </form>
                </td>                
            <tr>
            <tr>
                <td>Document Source :</td>
            <td>
                <form name="DocSource">
                     <select name="DocSource">
                        <option  value="-1">SRC_AUTO</option>
                        <option value="0">SRC_FLATBED</option>
                        <option value="1">SRC_FEEDER_SIMPLEX</option>
                        <option value="2">SRC_FEEDER_DUPLEX</option>
                     </select>
                </form>
             </td>
            </tr>
            <tr>
                <td>Resolution :</td>
            <td>
                <form name="Resolution">
                     <select name="Resolution">
                        <option value="200">200</option>
                        <option value="300">300</option>
                        <option value="600">600</option>
                     </select>
                </form>
             </td>
            <tr>
                <td>Size :</td>
                <td>
                        <form name="Size">
                            <select name="Size">
                                <option value="-1" selected="selected">DS_AUTO</option>
                                <option value="2">DS_A4</option>
                                <option value="3">DS_A5</option>
                            </select>
                        </form>
                </td>                
            <tr>
            </tr>
    </table>

    <input type="button" id="scan" value="Scan" disabled=true onclick="ScanButtonClick();"/><br />
    <br />
    <table>
    <tr>
        <td>File Format :</td>
        <td>
                <form name="fileFormat">
                    <select name="fileFormat">
                        <option>FF_BMP</option>
                        <option>FF_JPEG</option>
                        <option>FF_TIFF</option>
                        <option>FF_MULTITIFF</option>
                        <option selected="selected">FF_PDF</option>
                    </select>
                </form>
         </td>
    </tr>
    <tr>
        <td>Destination :</td>
        <td>
        <form name="destination">
          <select name="destination">
                 <option selected="selected">DEST_BASE64</option>  
                 <option>DEST_FILE</option>
           </select>
         </form>
        </td>
    </tr>
    </table>
    <br/>
    <input type="button" id="cancel" value="Cancel" disabled=true onclick="CancelButtonClick();"/><br />   
    <br />
    <input type="button" id="save"  disabled=true value="Save" onclick="SaveButtonClick();"/><br />
    <br />
    <input type="button" id="close"  disabled=true value="Close" onclick="CloseButtonClick();"/><br />
    <br />
    <input type="button" id="gserialNumber"  disabled=true value="Get serial Number" onclick="GetSerialButtonClick();"/><br />

    <div id="downloadarea">
    </div>

    <br />
    <textarea id="log"  cols="30" rows="50"  readonly></textarea>

    <br />

</body>
</html>